package lastfm
{
  import asunit.framework.TestCase;
  import lastfm.events.ApiEvent;

  /**
   * ...
   * @author Roman Marchenko
   */
  public class TestApi extends TestCase
  {
    public function TestApi(testMethod:String = null)
    {
      super(testMethod);
    }

    ///////////////////////////////////////////////////////////////////////////

    public function testArtistGetCorrection():void
    {
      var api:Api = new Api;
      var artist:String = "Guns and Roses";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertEquals("Guns N' Roses", event.response["artist"]);
        assertEquals("http://www.last.fm/music/Guns+N%27+Roses", event.response["url"]);
      };

      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.artistGetCorrection(artist);
    }

    ///////////////////////////////////////////////////////////////////////////

    public function testErrorArtistGetCorrection():void
    {
      var api:Api = new Api;
      var artist:String = "smsjfbsfbdmsfbmdsbfmdbfmdsbfm";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.response)
        assertNotNull(event.error)
        assertEquals(6, int(event.error["code"]));
      };

      api.addEventListener(ApiEvent.ERROR, addAsync(handler, 10000));
      api.artistGetCorrection(artist);
    }

    ///////////////////////////////////////////////////////////////////////////

    public function testArtistGetCorrectionCyr():void
    {
      var api:Api = new Api;
      var artist:String = "Чиж";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertEquals("", event.response["artist"]);
        assertEquals("", event.response["url"]);
      };

      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.artistGetCorrection(artist);
    }

    ///////////////////////////////////////////////////////////////////////////

    public function testArtistSearch():void
    {
      var api:Api = new Api;
      var artist:String = "Nickelback";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertNotNull(event.response["artistmatches"])
        assertNotNull(event.response["artistmatches"]["artist"])

        var matches:XMLList = event.response["artistmatches"]["artist"];
        assertTrue(0 < matches.length());
        assertEquals("Nickelback", matches[0]["name"]);
      };

      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.artistSearch(artist);
    }

    ///////////////////////////////////////////////////////////////////////////

    public function testArtistSearchCyr():void
    {
      var api:Api = new Api;
      var artist:String = "ддт";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertNotNull(event.response["artistmatches"])
        assertNotNull(event.response["artistmatches"]["artist"])

        var matches:XMLList = event.response["artistmatches"]["artist"];
        assertTrue(0 < matches.length());
        assertEquals("ДДТ", matches[0]["name"]);
      };

      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.artistSearch(artist);
    }

    ///////////////////////////////////////////////////////////////////////////

    public function testArtistSearchCyr2():void
    {
      var api:Api = new Api;
      var artist:String = "Чиж & Co";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertNotNull(event.response["artistmatches"])
        assertNotNull(event.response["artistmatches"]["artist"])

        var matches:XMLList = event.response["artistmatches"]["artist"];
        assertTrue(0 < matches.length());
        assertEquals("Чиж & Co", matches[0]["name"]);
      };

      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.artistSearch(artist);
    }

    ///////////////////////////////////////////////////////////////////////////

    public function testArtistSearchCyr3():void
    {
      var api:Api = new Api;
      var artist:String = "чиЖ";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertNotNull(event.response["artistmatches"])
        assertNotNull(event.response["artistmatches"]["artist"])

        var matches:XMLList = event.response["artistmatches"]["artist"];
        assertTrue(0 < matches.length());
        assertEquals("Чиж & Co", matches[0]["name"]);
      };

      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.artistSearch(artist);
    }

    ///////////////////////////////////////////////////////////////////////////

    public function testArtistGetSimilar():void
    {
      var api:Api = new Api;
      var artist:String = "Nickelback";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertEquals(artist, event.response["artist"]);
        assertNotNull(event.response["similar"]);

        assertEquals("Theory of a Deadman", event.response["similar"][0]);
        assertEquals("3 Doors Down", event.response["similar"][1]);
        assertEquals("Daughtry", event.response["similar"][2]);
      };

      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.artistGetSimilar(artist);
    }

    public function testArtistGetTrack():void
    {
      var api:Api = new Api;
      var artist:String = "Nickelback";
      var trackNum:int = 2;
      var trackTitle:String = "Far Away";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertEquals(artist, event.response["artist"]);
        assertEquals(trackTitle, event.response["track"]);
      };

      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.artistGetTrack(artist, 2);
    }

    public function testEncodedArtistName():void
    {
      var api:Api = new Api;
      var artist:String = "Florence + the Machine";
      var trackNum:int = 1;

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertEquals(artist, event.response["artist"]);
      };

      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.artistGetTrack(artist, 2);
    }

    public function testGetBannedTracks():void
    {
      var track:String = "Thank God For Muddy Waters";
      var artist:String = "Goofy Guys";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertEquals(track, event.response.bannedtracks.track[0].name);
        assertEquals(artist, event.response.bannedtracks.track[0].artist.name);
      };

      var api:Api = new Api;
      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.userGetBannedTracks("rj");
    }

    public function testGetBannedTracksPaged():void
    {
      var track:String = "Song for Athene";
      var artist:String = "The Choir of Temple Church";

      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertEquals(1, event.response.bannedtracks.track.length());
        assertEquals(track, event.response.bannedtracks.track[0].name);
        assertEquals(artist, event.response.bannedtracks.track[0].artist.name);
      };

      var api:Api = new Api;
      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.userGetBannedTracks("rj", 2, 1);
    }

    public function testGetTopTags():void
    {
      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error)
        assertNotNull(event.response)
        assertEquals("rock", event.response.toptags.tag[0].name.toString());
        assertTrue(0 < int(event.response.toptags.tag[0].count.toString()));
        assertNotNull(event.response.toptags.tag[0].url.toString());
      };

      var api:Api = new Api;
      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.tagGetTopTags();
    }

    public function testGetTopArtists():void
    {
      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error);
        assertNotNull(event.response);
        assertNotNull(event.response.topartists);
        assertNotNull(event.response.topartists.artist);
        assertTrue(0 < event.response.topartists.artist.length());
        assertNotNull(event.response.topartists.artist[0].name);
        assertTrue(0 < event.response.topartists.artist[0].name.toString().length);
      };

      var api:Api = new Api;
      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.chartGetTopArtists();
    }

    public function testGetToken():void
    {
      var handler:Function = function(event:ApiEvent):void
      {
        assertNull(event.error);
        assertNotNull(event.response);
        assertNotNull(event.response.token);
      };

      var api:Api = new Api;
      api.addEventListener(ApiEvent.OK, addAsync(handler, 10000));
      api.authGetToken();
    }
  }
}
